using System;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.IO;

namespace LogExceptions
{
    public class ApplicationLog
    {
        public ApplicationLog()
        {            
        }
                
        public void LogError(Exception exMe)
        {
            int nExceNumber = 0;
            MessageBox.Show(exMe.Message.ToString());
            string sExceptionDescription = FormatOrdinaryException(exMe, nExceNumber);
            WriteToFile(sExceptionDescription);            
        }

        public void LogError(SqlException sqexMe, int nExceNumber)
        {
            if (nExceNumber == 2)
            {
                MessageBox.Show("Can't connect to server, Contact System Administrator");
                return;
            }
            else if (nExceNumber == -2)
            {
                MessageBox.Show("Connection Timeout to Server, Close the application and try again");
                return;
            }
            else if (nExceNumber == -1073741769)
            {
                MessageBox.Show("Conection lost, Contact System Administrator");
                return;
            }
            else if (nExceNumber == 4060)
            {
                MessageBox.Show("Database Not Found");
                return;
            }
            else if (nExceNumber == 18456)
            {
                MessageBox.Show("Network Credentials Failed, Give Password");
                return;
            }
            else if (nExceNumber == 1326)
            {
                MessageBox.Show("Network Credentials Failed, Add Sql Port No or disable filewall");
                return;
            }
            else if (nExceNumber == 229)
            {
                MessageBox.Show("Credentials Check");
                return;
            }
            else if (nExceNumber == 208)
            {
                string sExceptionDescription = sqexMe.Message.Substring(20, sqexMe.Message.Length - 20);
                sExceptionDescription = sExceptionDescription.Substring(1, sExceptionDescription.Length - 3);
                sExceptionDescription = "Table " + sExceptionDescription + " not found";
                MessageBox.Show(sExceptionDescription);
                WriteToFile(sExceptionDescription);
                return;
            }
            else if (nExceNumber == 2601 || nExceNumber == 2627)
            {
                MessageBox.Show("Unique Key Voilation");
                string sExceptionDescription = FormatSqlException(sqexMe, nExceNumber);
                WriteToFile(sExceptionDescription);
                return;
            }
            else if (nExceNumber == 547)
            {
                MessageBox.Show("Foreign Key Violation");
                string sExceptionDescription = FormatSqlException(sqexMe, nExceNumber);
                WriteToFile(sExceptionDescription);
                return;
            }
            else if (nExceNumber == 2714 || nExceNumber == 1779)
            {
                MessageBox.Show("Already Created Tables for this table creation query");
                return;
            }
            else
            {                
                string sExceptionDescription = FormatSqlException(sqexMe, nExceNumber);
                MessageBox.Show(sExceptionDescription);
                WriteToFile(sExceptionDescription);                
            }                        
        }

        public void LogError(OdbcException sqexMe, int nExceNumber)
        {
            if (nExceNumber == 2)
            {
                MessageBox.Show("Can't connect to server, Contact System Administrator");
                return;
            }
            else if (nExceNumber == -2)
            {
                MessageBox.Show("Connection Timeout to Server, Close the application and try again");
                return;
            }
            else if (nExceNumber == -1073741769)
            {
                MessageBox.Show("Conection lost, Contact System Administrator");
                return;
            }
            else if (nExceNumber == 4060)
            {
                MessageBox.Show("Database Not Found");
                return;
            }
            else if (nExceNumber == 18456)
            {
                MessageBox.Show("Network Credentials Failed, Give Password");
                return;
            }
            else if (nExceNumber == 1326)
            {
                MessageBox.Show("Network Credentials Failed, Add Sql Port No or disable filewall");                
                return;
            }
            else if (nExceNumber == 229)
            {
                MessageBox.Show("Credentials Check");
                MessageBox.Show(sqexMe.Message.ToString());
                return;
            }
            else if (nExceNumber == 208)
            {
                MessageBox.Show("Tables not found");
                MessageBox.Show(sqexMe.Message.ToString());
                string sExceptionDescription = FormatSqlException(sqexMe, nExceNumber);
                WriteToFile(sExceptionDescription);
                return;
            }
            else if (nExceNumber == 2601 || nExceNumber == 2627)
            {
                MessageBox.Show("Unique Key Voilation");
                MessageBox.Show(sqexMe.Message.ToString());
                string sExceptionDescription = FormatSqlException(sqexMe, nExceNumber);
                WriteToFile(sExceptionDescription);
                return;
            }
            else if (nExceNumber == 547)
            {
                MessageBox.Show("Foreign Key Violation");
                MessageBox.Show(sqexMe.Message.ToString());
                string sExceptionDescription = FormatSqlException(sqexMe, nExceNumber);
                WriteToFile(sExceptionDescription);
                return;
            }
            else if (nExceNumber == 2714 || nExceNumber == 1779)
            {
                MessageBox.Show("Already Created Tables");
                MessageBox.Show(sqexMe.Message.ToString());
                string sExceptionDescription = FormatSqlException(sqexMe, nExceNumber);
                WriteToFile(sExceptionDescription);
                return;
            }
            else
            {
                MessageBox.Show(sqexMe.Message.ToString());
                string sExceptionDescription = FormatSqlException(sqexMe, nExceNumber);
                WriteToFile(sExceptionDescription);                
            }
        }

        protected static string FormatOrdinaryException(Exception exMe, int nExceNumber)
        {           
            StringBuilder sb = new StringBuilder();             
            sb.Append("Time of Error: " + DateTime.Now.ToString("g") + Environment.NewLine);
            if (exMe != null)
            {                
                sb.Append("Message: " + exMe.ToString() + Environment.NewLine);                
                sb.Append(Environment.NewLine);                
            }            
            return sb.ToString();
        }

        protected static string FormatSqlException(SqlException sqexMe, int nExceNumber)
        {
            StringBuilder sb = new StringBuilder();                                    
            if (sqexMe != null)
            {
                sb.Append("Time of Error: " + DateTime.Now.ToString("g") + Environment.NewLine);
                for (int i = 0; i < sqexMe.Errors.Count; i++)
                {
                    sb.Append("Index #" + i.ToString() + Environment.NewLine);
                    sb.Append("Message: " + sqexMe.Errors[i].Message  + Environment.NewLine);
                    sb.Append("LineNumber: " + sqexMe.Errors[i].LineNumber + Environment.NewLine);
                    sb.Append("Source: " + sqexMe.Errors[i].Source + Environment.NewLine);
                    sb.Append("Procedure: " + sqexMe.Errors[i].Procedure + Environment.NewLine);                                     
                }
                sb.Append(Environment.NewLine);   
            }            
            return sb.ToString();
        }

        protected static string FormatSqlException(OdbcException sqexMe, int nExceNumber)
        {
            StringBuilder sb = new StringBuilder();
            if (sqexMe != null)
            {
                sb.Append("Time of Error: " + DateTime.Now.ToString("g") + Environment.NewLine);
                for (int i = 0; i < sqexMe.Errors.Count; i++)
                {
                    sb.Append("Index #" + i.ToString() + Environment.NewLine);
                    sb.Append("Message: " + sqexMe.Errors[i].Message + Environment.NewLine);                    
                    sb.Append("Source: " + sqexMe.Errors[i].Source + Environment.NewLine);                                        
                }
                sb.Append(Environment.NewLine);
            }
            return sb.ToString();
        }

        public static void WriteToFile(string sText)
        {
            string sToday = DateTime.Today.Day + "-" + DateTime.Today.Month + "-" + DateTime.Today.Year;
            string sPath = AppDomain.CurrentDomain.BaseDirectory + "Errors";
            System.IO.Directory.CreateDirectory(@sPath);
            sPath = sPath + "\\" + sToday + ".txt";

            FileInfo fileInfo = new FileInfo(sPath);
            if (fileInfo.Exists)
            {
                FileStream fs = new FileStream(sPath, FileMode.Append, FileAccess.Write);
                StreamWriter writer = new StreamWriter(fs);
                writer.Write(sText);
                writer.Close();
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream(sPath, FileMode.CreateNew, FileAccess.Write);
                StreamWriter writer = new StreamWriter(fs);
                writer.Write(sText);
                writer.Close();
                fs.Close();
            }
            return;
        }

        public void WriteToQuery(string sText, string sName)
        {
            string sPath = AppDomain.CurrentDomain.BaseDirectory + "Errors";
            
            System.IO.Directory.CreateDirectory(@sPath);
            sPath = sPath + "\\" + sName + ".txt";

            FileInfo fileInfo = new FileInfo(sPath);
            if (fileInfo.Exists)
            {
                FileStream fs = new FileStream(sPath, FileMode.Append, FileAccess.Write);
                StreamWriter writer = new StreamWriter(fs);
                writer.Write(sText);
                writer.Close();
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream(sPath, FileMode.CreateNew, FileAccess.Write);
                StreamWriter writer = new StreamWriter(fs);
                writer.Write(sText);
                writer.Close();
                fs.Close();
            }
            return;
        }        
    }
}

